<?php

//小站选项应用模型Board模块
class Board_model extends Model {

	function Board_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("plugins_model", "pm");
	}
	
	//获取列表
	function listitem($offset = 0, $pagecount = 20, $order = "id", $desc = "desc")
	{
		$items = array();
		$query = $this->db->from("module_board")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
			foreach($items as $k => $list){
				$siteid = $this->pm->get_siteid($list["plid"]);
				$site = $this->sm->get($siteid);
				if($site) {
					$items[$k]["domain"] = $site["domain"];
					$items[$k]["sitename"] = $site["name"];
					$items[$k]["logo"] = $site["logo"];
				} else {
					$items[$k]["domain"] = $items[$k]["sitename"] = $items[$k]["logo"] = "";
				}
				
				//用户信息
				$userid = $list["userid"];
				$userinfo = $this->am->get($userid);
				$items[$k]["userinfo"] = $userinfo;
			}
		}
		return $items;
	}
	
	//计算总数
	function countitems()
	{
		return $this->db->count_all_results("module_board");
	}
	
	//添加
	function add($plid)
	{
		$description = p("description");
		if(trim($description) == "") {
			show_failed("请输入内容");
		}
		$data = array(
			"userid" => $this->userid,
			"description" => $description,
			"plid" => $plid,
			"addtime" => time(),
		);
		$result = $this->db->insert("module_board", $data);
		$insert_id = $this->db->insert_id();
		return $insert_id;
	}
	
	//#获取列表
	function lists($plid, $limit = 1)
	{
		$lists = array();
		$query = $this->db->from("module_board")->where(array("plid"=>$plid))->order_by("id","desc")->limit($limit)->get();
		if($query->num_rows()>0) {
			$lists = $query->row_array();
			//模块ID
			$plid = $lists["plid"];
			$q = $this->db->get_where("site_plugins", array("id"=>$plid));
			$r = $q->row_array();
			$lists["moduleid"] = $r["moduleid"];
		}
		return $lists;
	}
	
	function delete($dataid)
	{
		$result = $this->db->delete("module_board", array("id"=>$dataid));
		return $result;
	}
	
	//删除选择话题
	function delselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("module_board");
		return $result;
	}
	
	//#查看内容
	function show($id)
	{
		$row = array();
		$query = $this->db->get_where("module_board", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			$r = $this->pm->get($row["plid"]);
			$row["moduleid"] = $r["moduleid"];
			$opid = $r["opid"];
			$r = $this->options_model->get($opid);
			if($r) {
				$siteid = $r["siteid"];
				//小站信息
				$row["site"] = $this->sm->get_site($siteid);
			} else {
				$row["site"]["name"] = '';
			}
			//用户信息
			$userid = $row["userid"];
			$userinfo = $this->am->get($userid);
			$row["userinfo"] = $userinfo;
			
		}
		return $row;
	}
	
	//获取应用的公告内容
	function get($plid)
	{	
		$row = array();
		$query = $this->db->from("module_board")->where("plid", $plid)->order_by("id", "desc")->limit(1)->get();
		if ($query->num_rows()>0) {
			$row = $query->row_array();
		}
		return $row;
	}
	
}

/* End of file */